package csu.web.mypetstore.persistence.impl;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.persistence.DBUtil;
import csu.web.mypetstore.persistence.LogDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class LogDaoImpl implements LogDao {
    private static final String INSERTLOG = "INSERT INTO LOG (USERNAME, INFO) VALUES (?, ?)";
    private static final String FINDALLLOGS = "SELECT INFO FROM LOG WHERE USERNAME = ?";
    @Override
    public void insertLog(String username, String logInfo) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(INSERTLOG);
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,logInfo);
            preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    @Override
    public List<String> getLogInfoByUsername(Account account) {
        List<String> logList = new ArrayList<>();
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(FINDALLLOGS);
            preparedStatement.setString(1,account.getUsername());
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                String logInfo = resultSet.getString(1);
                logList.add(logInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return logList;
    }
}
